package org.rulix.ogame.player.core;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class OgameRequestFactory {
	
	public static HttpPost getLoginRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_LOGIN, ogameSession.getSessionId());
		sb.append(url);
		
		HttpPost httpost = new HttpPost(sb.toString());
           
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("uni_id", ""));
		nvps.add(new BasicNameValuePair("kid", ""));
		nvps.add(new BasicNameValuePair("is_utf8", "0"));
		nvps.add(new BasicNameValuePair("uni_url", ogameSession.getOgameServer()));
		nvps.add(new BasicNameValuePair("login", ogameSession.getLogin()));
		nvps.add(new BasicNameValuePair("pass", ogameSession.getPassword()));
 
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create login request", e);
		}
		return httpost;
	}
	
	public static HttpRequest getOverviewRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_OVERVIEW, ogameSession.getSessionId());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpRequest getFetchReourcesRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
			String url = String.format(OgamePaths.OGAME_PATH_FETCH_RESOURCES, ogameSession.getSessionId());
			sb.append(url);

		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpRequest getMessagesRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
			String url = String.format(OgamePaths.OGAME_PATH_MESSAGES, ogameSession.getSessionId());
			sb.append(url);
		
		HttpPost httpost = new HttpPost(sb.toString());
           
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("displayCategory", "9"));
		nvps.add(new BasicNameValuePair("displayPage", "1"));
		nvps.add(new BasicNameValuePair("ajax", "1"));

		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create messagess request", e);
		}
		return httpost;
	}
	
	
	//OGAME_PATH_EVENTLIST
	public static HttpGet getEventListRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
			String url = String.format(OgamePaths.OGAME_PATH_EVENTLIST, ogameSession.getSessionId());
			sb.append(url);
		
		HttpGet httpGet = new HttpGet(sb.toString());
		return httpGet;
	}
	
	
	
	
}
